package com.screen.screendisplay.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.screen.screendisplay.common.pojo.ApiResponse;
import com.screen.screendisplay.entity.dryport.DryportProdoperGoodsSend;
import com.screen.screendisplay.entity.dryport.DryportProdoperOwnCoal;
import com.screen.screendisplay.service.dryport.DryportProdoperGoodsSendService;
import com.screen.screendisplay.service.dryport.DryportProdoperOwnCoalService;
import com.screen.screendisplay.service.dryport.DryportProdoperFluxPlanService;
import com.screen.screendisplay.service.dryport.DryportProdoperTurnoverService;
import com.screen.screendisplay.service.dryport.DryportProdoperPortGatherService;
import com.screen.screendisplay.service.dryport.DryportProdoperThroughputService;
import com.screen.screendisplay.service.dryport.DryportProdoperTransshipmentService;
import com.screen.screendisplay.service.dryport.DryportOverviewItemService;
import com.screen.screendisplay.service.dryport.DryportOpermonLocomotiveService;
import com.screen.screendisplay.service.dryport.DryportOpermonCarService;
import com.screen.screendisplay.service.dryport.DryportOpermonShipService;
import com.screen.screendisplay.service.dryport.DryportSafetyenvLineService;
import com.screen.screendisplay.service.dryport.DryportSafetyenvSkylightService;
import com.screen.screendisplay.vo.dryport.*;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
/**
 * <p>
 * 路港生产经营 前端控制器
 * </p>
 *
 * @author xuesong
 * @since 2025-08-23
 */
@Tag(name = "路港生产经营", description = "路港生产经营")
@RestController
@RequestMapping("/dryport")
public class DryportController {
    
    @Autowired
    private DryportProdoperGoodsSendService goodsSendService;
    
    @Autowired
    private DryportProdoperOwnCoalService ownCoalService;
    
    @Autowired
    private DryportProdoperFluxPlanService dryportProdoperFluxPlanService;
    
    @Autowired
    private DryportProdoperTurnoverService dryportProdoperTurnoverService;
    
    @Autowired
    private DryportProdoperPortGatherService dryportProdoperPortGatherService;
    
    @Autowired
    private DryportProdoperThroughputService dryportProdoperThroughputService;
    
    @Autowired
    private DryportProdoperTransshipmentService dryportProdoperTransshipmentService;
    
    @Autowired
    private DryportOverviewItemService dryportOverviewItemService;
    
    @Autowired
    private DryportOpermonLocomotiveService dryportOpermonLocomotiveService;
    
    @Autowired
    private DryportOpermonCarService dryportOpermonCarService;
    
    @Autowired
    private DryportOpermonShipService dryportOpermonShipService;
    
    @Autowired
    private DryportSafetyenvLineService dryportSafetyenvLineService;
    
    @Autowired
    private DryportSafetyenvSkylightService dryportSafetyenvSkylightService;
    
    /**
     * 生产经营-货物发送周转情况-自有煤炭情况
     * @return
     */
    @Operation(summary = "生产经营-货物发送周转情况-自有煤炭情况", description = "生产经营-货物发送周转情况-自有煤炭情况")
    @GetMapping(value = "/operation/goods-turnover/statistics")
    public ApiResponse<List<GoodsSendVO>> goodsSendStatistics(
        @RequestParam(required = false) Integer year
    ) {
        if (year == null) {
            year = 2025;
        }
        // 从数据库查询数据
        List<DryportProdoperGoodsSend> entityList = goodsSendService.getMonthlyStatsByYear(year);
        
        // 转换为 VO 对象
        List<GoodsSendVO> data = entityList.stream().map(entity -> {
            GoodsSendVO vo = new GoodsSendVO();
            vo.setDate(entity.getDate());
            vo.setGoodsSendPlan(entity.getGoodsSendPlan());
            vo.setGoodsSendAc(entity.getGoodsSendAc());
            vo.setGoodsSendCompRate(entity.getGoodsSendCompRate());
            return vo;
        }).collect(Collectors.toList());
        
        return ApiResponse.succeed(data);
    }

    /**
     * 生产经营-自有煤炭情况
     * @return
     */
    @Operation(summary = "生产经营-自有煤炭情况", description = "生产经营-自有煤炭情况")
    @GetMapping(value = "/operation/goods-turnover/own-coal")
    public ApiResponse<List<OwnCoalVO>> ownCoalStatistics(
        @RequestParam(required = false) Integer year) {
        if (year == null) {
            year = 2025;
        }
        // 从数据库查询数据
        List<DryportProdoperOwnCoal> entityList = ownCoalService.getMonthlyStatsByYear(year);
        
        // 转换为 VO 对象
        List<OwnCoalVO> data = entityList.stream().map(entity -> {
            OwnCoalVO vo = new OwnCoalVO();
            vo.setYear(entity.getYear());
            vo.setMonth(entity.getMonth());
            vo.setDate(entity.getDate());
            vo.setOwnCoalPlanned(entity.getOwnCoalPlanned());
            vo.setOwnCoalCompleted(entity.getOwnCoalCompleted());
            vo.setOwnCompletionRate(entity.getOwnCompletionRate());
            return vo;
        }).collect(Collectors.toList());
        
        return ApiResponse.succeed(data);
    }

    /**
     * 生产经营-引流计划统计
     * @param year 年份参数
     * @return
     */
    @Operation(summary = "生产经营-引流/非煤计划统计, type：0 引流计划 1：非煤计划", description = "生产经营-引流计划统计")
    @GetMapping(value = "/operation/goods-turnover/drainage-plan")
    public ApiResponse<List<FluxPlanVO>> fluxPlanStatistics(
        @RequestParam(required = true) Integer year,
        @RequestParam(required = true) Integer type) {
        // 从数据库查询数据
        List<FluxPlanVO> data = dryportProdoperFluxPlanService.getMonthlyStatsByYearAndType(year, type);
        return ApiResponse.succeed(data);
    }

    /**
     * 生产经营-周转计划情况
     * @param year 年份参数
     * @return
     */
    @Operation(summary = "生产经营-周转计划情况", description = "生产经营-周转计划情况")
    @GetMapping(value = "/operation/goods-turnover/turnover-plan")
    public ApiResponse<List<TurnoverVO>> turnoverStatistics(@RequestParam(required = false) Integer year) {
        // 从数据库查询数据
        List<TurnoverVO> data = dryportProdoperTurnoverService.getMonthlyStatsByYear(year);
        return ApiResponse.succeed(data);
    }

    @Operation(summary = "生产经营-港口集疏港量完成情况-集港量", description = "生产经营-港口集疏港量完成情况-集港量")
    @GetMapping(value = "/operation/port-volume/collection")
    public ApiResponse<List<PortGatherVO>> portGatherStatistics(@RequestParam(required = true) Integer year) {
        // 从数据库查询数据
        List<PortGatherVO> data = dryportProdoperPortGatherService.getMonthlyStatsByYear(year);
        return ApiResponse.succeed(data);
    }


    @Operation(summary = "生产经营-港口集疏港量完成情况-吞吐量", description = "生产经营-港口集疏港量完成情况-吞吐量")
    @GetMapping(value = "/operation/port-volume/throughput")
    public ApiResponse<List<ThroughputVO>> throughputStatistics(@RequestParam(required = true) Integer year) {
        // 从数据库查询数据
        List<ThroughputVO> data = dryportProdoperThroughputService.getMonthlyStatsByYear(year);
        return ApiResponse.succeed(data);
    }

    @Operation(summary = "生产经营-港口集疏港量完成情况-倒运量", description = "生产经营-港口集疏港量完成情况-倒运量")
    @GetMapping(value = "/operation/port-volume/transshipment")
    public ApiResponse<List<TransshipmentVO>> transshipmentStatistics(@RequestParam(required = true) Integer year) {
        // 从数据库查询数据
        List<TransshipmentVO> data = dryportProdoperTransshipmentService.getMonthlyStatsByYear(year);
        return ApiResponse.succeed(data);
    }

    @Operation(summary = "产业指标-产业顶部指标", description = "产业指标-产业顶部指标")
    @GetMapping(value = "/overview/main-indicators")
    public ApiResponse<OverviewVO> overviewStatistics(@RequestParam(required = true) Integer year) {
        // 从数据库查询数据
        OverviewVO data = dryportOverviewItemService.getStatsByYear(year);
        return ApiResponse.succeed(data);
    }

    @Operation(summary = "运行监测-设备运行监测-机车整体运行情况", description = "运行监测-设备运行监测-机车整体运行情况")
    @GetMapping(value = "/monitoring/equipment/locomotive-status")
    public ApiResponse<LocomotiveStatusVO> locomotiveStatus(@RequestParam(value = "year", required = false) Integer year) {
        if (year == null) {
            year = 2025;
        }
        // 调用Service获取数据
        LocomotiveStatusVO response = dryportOpermonLocomotiveService.getLocomotiveStatus(year);
        return ApiResponse.succeed(response);
    }


    @Operation(summary = "运行监测-设备运行监测-车辆运行情况监视", description = "运行监测-设备状态监测-车辆运行情况监视")
    @GetMapping(value = "/monitoring/equipment/vehicle-status")
    public ApiResponse<VehicleStatusVO> vehicleStatus(@RequestParam(value = "year", required = false) Integer year) {
        // 调用Service获取数据
        VehicleStatusVO response = dryportOpermonCarService.getVehicleStatus(year);
        return ApiResponse.succeed(response);
    }


    @Operation(summary = "运行监测-设备状态监测-来船信息", description = "运行监测-设备状态监测-来船信息")
    @GetMapping(value = "/shipInfoStatistics")
    public ApiResponse<List<ShipVO>> shipInfoStatistics(@RequestParam(required = false) Integer year) {
        if (year == null) {
            year = 2025;
        }

        // 调用Service获取数据
        List<ShipVO> data = dryportOpermonShipService.getShipInfoStatistics(year);
        return ApiResponse.succeed(data);
    }


    @Operation(summary = "安全环保-施工检修-营业线路计划情况", description = "安全环保-施工检修-营业线路计划情况")
    @GetMapping(value = "/lineStatistics")
    public ApiResponse<List<LineVO>> lineStatistics(@RequestParam(required = true) Integer year) {
        // 调用Service获取数据
        List<LineVO> data = dryportSafetyenvLineService.getLineStatistics(year);
        return ApiResponse.succeed(data);
    }

    // @Operation(summary = "安全环保-施工检修-今日计划兑现", description = "施工检修-节煤减排量-今日计划兑现")
    // @GetMapping(value = "/m14")
    // public ApiResponse<Object> m14(@RequestParam(value = "year", required = false) Integer year) {
    //     return ApiResponse.succeed();
    // }

    @Operation(summary = "安全环保-天窗修", description = "安全环保-天窗修")
    @GetMapping(value = "/skylightStatistics")
    public ApiResponse<SkylightVO> skylightStatistics(@RequestParam(required = true) Integer year) {
        // 调用Service获取数据
        SkylightVO vo = dryportSafetyenvSkylightService.getSkylightStatistics(year);
        return ApiResponse.succeed(vo);
    }

    @Operation(summary = "安全环保-装车卸车完成情况", description = "安全环保-装车卸车完成情况")
    @GetMapping(value = "/m16")
    public ApiResponse<Object> m16(@RequestParam(value = "year", required = false) Integer year) {
        return ApiResponse.succeed();
    }

    @Operation(summary = "天气信息", description = "获取港口天气信息，包含日夜平均湿度、水温状况等")
    @GetMapping(value = "/weather/info")
    public ApiResponse<WeatherInfoVO> getWeatherInfo(@RequestParam(value = "unitCode", required = false) String unitCode) {
        // Mock数据
        WeatherInfoVO vo = new WeatherInfoVO();
        vo.setDayNightHumidity("日夜平均湿度：重雾天气5年");
        vo.setWaterTemperature("水温天气区域：阴天相距");
        return ApiResponse.succeed(vo);
    }

    @Operation(summary = "列车实时信息", description = "获取列车实时信息，包含机车数、人员数、风速、速度等")
    @GetMapping(value = "/train/realtime")
    public ApiResponse<TrainRealtimeInfoVO> getTrainRealtimeInfo(@RequestParam(value = "unitCode", required = false) String unitCode) {
        // Mock数据
        TrainRealtimeInfoVO vo = new TrainRealtimeInfoVO();
        vo.setLocomotiveCount(8321);
        vo.setPersonnelCount(6320);
        vo.setWindSpeed(new BigDecimal("55"));
        vo.setTrainSpeed(new BigDecimal("275"));
        vo.setCargoLoaded(4826);
        return ApiResponse.succeed(vo);
    }

    @Operation(summary = "列车运行信息", description = "获取列车运行信息，包含车次、速度、里程等")
    @GetMapping(value = "/train/operation")
    public ApiResponse<TrainOperationInfoVO> getTrainOperationInfo(@RequestParam(value = "unitCode", required = false) String unitCode) {
        // Mock数据
        TrainOperationInfoVO vo = new TrainOperationInfoVO();
        List<TrainOperationInfoVO.TrainItem> trainList = new ArrayList<>();
        
        // 添加5条列车运行数据
        String[] trainNumbers = {"8321", "8322", "8323", "8324", "8325"};
        String[] speeds = {"正常运行", "正常运行", "正常运行", "较高速", "正常运行"};
        String[] mileages = {"56.8 km", "45.2 km", "78.5 km", "32.1 km", "91.3 km"};
        
        for (int i = 0; i < trainNumbers.length; i++) {
            TrainOperationInfoVO.TrainItem item = new TrainOperationInfoVO.TrainItem();
            item.setTrainNumber(trainNumbers[i]);
            item.setSpeed(speeds[i]);
            item.setMileage(mileages[i]);
            trainList.add(item);
        }
        
        vo.setTrainList(trainList);
        return ApiResponse.succeed(vo);
    }

    @Operation(summary = "铁路运量信息", description = "获取铁路运量信息，包含日间、夜间、本月日均运量")
    @GetMapping(value = "/railway/transport")
    public ApiResponse<RailwayTransportInfoVO> getRailwayTransportInfo(@RequestParam(value = "unitCode", required = false) String unitCode) {
        // Mock数据
        RailwayTransportInfoVO vo = new RailwayTransportInfoVO();
        vo.setDaytimeTransport(new BigDecimal("7.06"));
        vo.setNightTransport(new BigDecimal("7.26"));
        vo.setMonthlyAvgTransport(new BigDecimal("36.47"));
        return ApiResponse.succeed(vo);
    }

    @Operation(summary = "铁路TQI", description = "获取铁路TQI指标，包含各月份TQI值")
    @GetMapping(value = "/railway/tqi")
    public ApiResponse<RailwayTqiVO> getRailwayTqi(@RequestParam(value = "unitCode", required = false) String unitCode) {
        // Mock数据
        RailwayTqiVO vo = new RailwayTqiVO();
        List<RailwayTqiVO.TqiMonthItem> tqiDataList = new ArrayList<>();
        
        // 添加6个月的TQI数据
        String[] months = {"1月", "2月", "3月", "4月", "5月", "6月"};
        BigDecimal[] values = {
            new BigDecimal("7.0"), 
            new BigDecimal("6.9"), 
            new BigDecimal("9.5"), 
            new BigDecimal("14.5"), 
            new BigDecimal("18.5"), 
            new BigDecimal("21.5")
        };
        
        for (int i = 0; i < months.length; i++) {
            RailwayTqiVO.TqiMonthItem item = new RailwayTqiVO.TqiMonthItem();
            item.setMonth(months[i]);
            item.setTqiValue(values[i]);
            tqiDataList.add(item);
        }
        
        vo.setTqiDataList(tqiDataList);
        return ApiResponse.succeed(vo);
    }

    @Operation(summary = "港口信息", description = "获取港口动态信息")
    @GetMapping(value = "/port/info")
    public ApiResponse<PortInfoVO> getPortInfo(@RequestParam(value = "unitCode", required = false) String unitCode) {
        // Mock数据
        PortInfoVO vo = new PortInfoVO();
        List<PortInfoVO.PortNewsItem> portNewsList = new ArrayList<>();
        
        // 添加港口动态信息
        PortInfoVO.PortNewsItem item1 = new PortInfoVO.PortNewsItem();
        item1.setPublishDate("2025年7月16日");
        item1.setTitle("白音华至锦州港区线路提速改造工程(白音华至段)");
        item1.setContent("2025年6月3日顺利完成了\"白音华线\"霍青改区工程，为原线铁公共2.17万吨...");
        portNewsList.add(item1);
        
        PortInfoVO.PortNewsItem item2 = new PortInfoVO.PortNewsItem();
        item2.setPublishDate("2025年8月18日");
        item2.setTitle("霍青改造工程\"五高运输\"整改进展");
        item2.setContent("霍青改造工程：15,642万吨");
        portNewsList.add(item2);
        
        vo.setPortNewsList(portNewsList);
        return ApiResponse.succeed(vo);
    }

    
}
